package dgut.haitaobao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dgut.haitaobao.entry.User;
import dgut.haitaobao.mapper.UserMapper;
import dgut.haitaobao.service.UserService;
import dgut.haitaobao.util.MD5Utils;
import dgut.haitaobao.util.response.HttpResponse;
import dgut.haitaobao.vo.param.UserLoginOrRegisterParamsVO;
import dgut.haitaobao.vo.result.LoginOrRegisterResultVO;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
/*具有互斥性*/
@Scope("singleton")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public HttpResponse userLogin(String name, String password) {
        QueryWrapper<User> queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",name).eq("password",password);
        User user = userMapper.selectOne(queryWrapper);
        if (user!=null){
            ////2.如果没有被注册则进行保存操作
            ////3.登录成功生成token,并返回给前端
            //String token= Base64Utils.encode(name+"1234");
            //return HttpResponse.success("账号密码正确",token);

            /*
              1.验证成功生成令牌token
              2.使用jwt规则生成token字符串
            */
            JwtBuilder builder= Jwts.builder();
            String haitaobaoToken = builder.setSubject(name) //主题就是token中携带的数据
                    .setIssuedAt(new Date())//设置token生成的时间
                    .setId(user.getUserId().toString()) //设置用户id为token
                    .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))//设置token的过期时间
                    .signWith(SignatureAlgorithm.HS256, "haitaobao") //设置加密方式和加密密码-只有知道密码(haitaobao)才能加密和解密
                    .compact();

            LoginOrRegisterResultVO userLogin = new LoginOrRegisterResultVO();
            userLogin.setToken(haitaobaoToken);
            userLogin.setUser(user);
            return HttpResponse.success(userLogin);
        }
        return HttpResponse.error("账号或密码错误");
    }

    @Override
    @Transactional
    public HttpResponse userResgit(String name, String password) {
        synchronized (this) {
            //1.根据用户查询，这个用户是否已经被注册
            QueryWrapper<User> queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", name);
            User user = userMapper.selectOne(queryWrapper);
            if (user == null) {
                //2.如果没有被注册则进行保存操作
                User resgitUser = new User();
                resgitUser.setUserName(name);
                resgitUser.setPassword(password);
                resgitUser.setUserRegtime(new Date());
                resgitUser.setUserImag("img/default.png");
                resgitUser.setUserModtime(new Date());
                int falg = userMapper.insert(resgitUser);
                if (falg > 0) {
                    return HttpResponse.success("用户注册成功");
                } else {
                    return HttpResponse.error("用户注册失败");
                }
            }
            return HttpResponse.error("注册失败,用户账号已存在");
        }
    }
}
